<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 日程表控制类-入口
 *@author liubin-bt
 *@property Schedule_model $Schedule_model
 *@property ScheduleUser_model $ScheduleUser_model
 *@property User_model $User_model
 *@property Project_model $Project_model
 *@property ProjectFollows_model $ProjectFollows_model
 */
class Schedule extends Base {
    
    public function __construct(){
        parent::__construct();
        $this->load->model('Schedule_model');
    }
    /**
     * 日程列表筛选
     */
    public function index(){
        $this->load->model('Project_model');
        $uid = $this->user['uid'];
        $project_id = intval($this->getSafeRequest('project_id', 0));
        $month = $this->getSafeRequest('select_time', date('Ym01'));
        if(strlen($month) == 6){
            $month = $month.'01';
        }
        $list = $this->Schedule_model->getList($uid, $project_id, $month);
        if( $list ){
            $project_ids = array_unique(array_column($list, 'project_id'));
            $projects = $this->Project_model->getProjectByIds($project_ids);
            foreach($list as &$v){
                $v['project_name'] = $projects[$v['project_id']]['project_name'];
                $v['start'] = date('Y-m-d H:i:s', $v['start']);
                $v['end'] = date('Y-m-d H:i:s', $v['end']);
                $v['colorType'] = $v['color_type'];
                unset($v['color_type']);
            }
        }
        log_message('debug', "Schedule.index \t uid:".$uid." \t request:".json_encode($_REQUEST)." \t res:".json_encode($list));
        $this->outJsonData(0, $list);
    }
    /**
     * 日程详情
     */
    public function detail(){
        $this->load->model('ScheduleUser_model');
        $this->load->model('User_model');
        $this->load->model('Project_model');
        $id = intval($this->getSafeRequest('id', 0));
        if( empty($id) ){
            $this->outJsonData(-1, '日程id不能为空');
        }
        $data = $this->Schedule_model->getDetail($id);
        if( !$data ){
            $this->outJsonData(-1, '该日程记录不存在');
        }
        //项目详情获取
        $project = $this->Project_model->getProjectOne($data['project_id']);
        $data['project_name'] = isset($project['project_name']) ? $project['project_name'] : '';
        //users获取
        $schedule_user = $this->ScheduleUser_model->getUidList($id);
        $users = [];
        $uids = [];
        if( $schedule_user ){
            $uids = array_column($schedule_user, 'uid');
            $uids_status = [];
            foreach ($schedule_user as $v){
                $uids_status[$v['uid']] = $v['status'];
            }
            $users = $this->User_model->getUserByUids($uids);
            foreach ($users as &$u){
                $u['lable'] = $u['user_name'];
                unset($u['user_name']);
                $u['joinstatus'] = $uids_status[$u['id']];
            }
        }
        $data['start'] = date('Y-m-d H:i:s', $data['start']);
        $data['end'] = date('Y-m-d H:i:s', $data['end']);
        $data['user'] = $users;
        $uid = $this->user['uid'];
        $data['isjoin'] = 0;
        $data['joinstatus'] = 0;
        if( in_array($uid, $uids) ){
            $data['isjoin'] = 1;
            $data['joinstatus'] = $uids_status[$uid];
        }
        log_message('debug', "Schedule.detail \t uid:".$uid." \t request:".json_encode($_REQUEST)." \t res:".json_encode($data));
        $this->outJsonData(0, $data);
    }
    /**
     * 添加编辑日程
     */
    public function add(){
        $this->load->model('ScheduleUser_model');
        $postdata = file_get_contents('php://input');
        if( $postdata ){
            $_POST = json_decode($postdata, true);
        }
        if( $_POST ){
            $schedule_id = $this->getSafePost('id', '');
            $title = $this->getSafePost('title', '');
            $project_id = intval($this->getSafePost('project_id', 0));
            $stime = strtotime($this->getSafePost('start', 0));
            $etime = strtotime($this->getSafePost('end', 0));
            $location = $this->getSafePost('location', '');
            $description = $this->getSafePost('description', '');
            $uids = $this->getSafePost('uids', '');
            if( empty($title) ){
                $this->outJsonData(-1, '日程名称不能为空');
            }
            if( empty($project_id) ){
                $this->outJsonData(-1, '项目不能为空');
            }
            if( empty($stime) ){
                $this->outJsonData(-1, '开始时间不能为空');
            }
            if( empty($etime) ){
                $this->outJsonData(-1, '结束时间不能为空');
            }
            if( $stime > $etime ){
                $this->outJsonData(-1, '开始时间不能大于结束时间');
            }
            if( empty($location) ){
                $this->outJsonData(-1, '地点不能为空');
            }
            if( !$this->auth_invaid($project_id) ){
                $this->outJsonData(-1, '没有权限操作');
            }
            $data['uid'] = $this->user['uid'];
            $data['title'] = $title;
            $data['project_id'] = $project_id;
            $data['start'] = $stime;
            $data['end'] = $etime;
            $data['location'] = $location;
            $data['description'] = $description;
            $data['ctime'] = $_SERVER['REQUEST_TIME'];
            //编辑逻辑
            if( $schedule_id ){
                $res = $this->Schedule_model->update('id='.$schedule_id, $data);
                if( $res ){
                    $uids = explode(',', $uids);
                    //用户uid校验   --todo
                    
                    $this->ScheduleUser_model->update_batch($schedule_id, $uids);
                }
                log_message('debug', "Schedule.edit \t uid:".$data['uid']." \t request:".$postdata." \t res:".json_encode($res));
                $this->outJsonData(0, '编辑成功');
            } else {
                //添加逻辑
                $schedule_id = $this->Schedule_model->insert_db($data);
                if( $schedule_id ){
                    if( $uids ){
                        $uids = explode(',', $uids);
                        $uids = array_filter($uids);
                        $uids = array_unique($uids);
                        //用户uid校验   --todo
                
                        $list = [];
                        foreach ($uids as $uid){
                            $list[] = ['schedule_id'=>$schedule_id, 'uid'=>intval($uid), 'ctime'=>$_SERVER['REQUEST_TIME']];
                        }
                        $res = $this->ScheduleUser_model->insert_batch($list);
                    }
                }
                log_message('debug', "Schedule.add \t uid:".$data['uid']." \t request:".$postdata." \t res:".json_encode($res));
                $this->outJsonData(0, '添加成功');
            }
        }
        $this->outJsonData(-1, '非法请求');
    }
    
    /**
     * 修改个人日程状态
     */
    public function joinStatus(){
        $this->load->model('ScheduleUser_model');
        $schedule_id = intval($this->getSafeRequest('id', ''));
        $status = intval($this->getSafeRequest('status', 0));
        if( !$schedule_id ){
            $this->outJsonData(-1, '日程id不能为空');
        }
        if( !in_array($status, [1, 2, 3]) ){
            $this->outJsonData(-1, 'status值非法');
        }
        $map['uid'] = $this->user['uid'];
        $map['schedule_id'] = $schedule_id;
        $res = $this->ScheduleUser_model->update($map, ['status'=>$status]);
        log_message('debug', "schedule.joinStatus \t uid:".$map['uid']." \t request:".json_encode($_REQUEST)." \t res:".json_encode($res));
        $this->outJsonData(0, '修改成功');
    }
    /**
     * 删除日程
     */
    public function delete(){
        $schedule_id = intval($this->getSafeRequest('id', ''));
        $this->load->model('ScheduleUser_model');
        if( !$schedule_id ){
            $this->outJsonData(-1, '日程id不能为空');
        }
        $schduleinfo = $this->Schedule_model->get($schedule_id);
        if( !$schduleinfo ){
            $this->outJsonData(-1, '该日程不存在');
        }
        if( !$this->auth_invaid($schduleinfo['project_id']) ){
            $this->outJsonData(-1, '没有权限操作');
        }
        $res = $this->Schedule_model->delete($schedule_id);
        if( $res ){
            $this->ScheduleUser_model->delete(['schedule_id'=>$schedule_id]);
        }
        log_message('debug', "Schedule.delete \t uid:".$this->user['uid']." \t request:".json_encode($_REQUEST)." \t res:".json_encode($res));
        $this->outJsonData(0, '删除成功');
    }
}
?>